#!/bin/bash

#####################################
#        SVN HOOK UTILS             #
# Author: jayson 274851@qq.com      #
#####################################

## Config

APPID=$$

SVN_OP_USER=support
SVN_OP_PWD='PPNZMyYmS2DKf26r'
SVN_RESP=svn://192.168.40.16/mz
SVN_MATCH_PATTERN="maps/map_data"
XML_MATCH_PATTERN="scene.xml"

SVN_BIN=/usr/bin/svn
SVN_PWD_OPTIONS="--username $SVN_OP_USER --password $SVN_OP_PWD --no-auth-cache"
SVN_DIFF_OPTIONS="$SVN_PWD_OPTIONS --notice-ancestry --summarize"
SVN_EXPORT_OPTIONS="$SVN_PWD_OPTIONS --non-interactive --force"
SVN_CHECKOUT_OPTIONS="$SVN_PWD_OPTIONS --non-interactive --force"
SVN_COMMIT_OPTIONS="$SVN_PWD_OPTIONS --non-interactive -m autosubmit"

EXPORT_DATA_REPS="$SVN_RESP/flash/trunk/resources/pub/maps/map_data"
EXPORT_XML_REPS="$SVN_RESP/flash/trunk/resources/zh_CN/config/scene/scene.xml"

TARGET_COMMIT_REPS="$SVN_RESP/java/trunk/mz/src/main/resources/scenedata"

DEST_PATH="/data/mz_data/xx_s0/socket/conf/scenedata"

TEMP_PATH="/data/svn_data/temp/svnhook"

## Config(END)

REV=$1
PREV=$((REV-1))

log() {
	echo "$(date +%Y-%m-%d' '%H:%M:%S) - $1"
}

svn_data_export() {
	# export to executing env.
	
    $SVN_BIN export $SVN_EXPORT_OPTIONS $EXPORT_DATA_REPS $DEST_PATH 1>/dev/null
    chown -R www.www $DEST_PATH

	# submit to server-side
	RAND=`date +%N`
	SRV_TEMP_PATH="${TEMP_PATH}/mz_$RAND"
	
	rm -rf $SRV_TEMP_PATH
	mkdir $SRV_TEMP_PATH
	
	$SVN_BIN checkout $SVN_CHECKOUT_OPTIONS $TARGET_COMMIT_REPS $SRV_TEMP_PATH
	sleep 1
	
	$SVN_BIN export $SVN_EXPORT_OPTIONS $EXPORT_DATA_REPS $SRV_TEMP_PATH
	sleep 1
	
	$SVN_BIN commit $SVN_COMMIT_OPTIONS $SRV_TEMP_PATH
	rm -rf $SRV_TEMP_PATH
}

svn_xml_export() {
	# export to executing env.
	
    $SVN_BIN export $SVN_EXPORT_OPTIONS $EXPORT_XML_REPS $DEST_PATH/scene.xml
    chown -R www.www $DEST_PATH/scene.xml

	# submit to server-side
	RAND=`date +%N`
	SRV_TEMP_PATH="${TEMP_PATH}/mz_$RAND"
	rm -rf $SRV_TEMP_PATH
	mkdir $SRV_TEMP_PATH
	
	$SVN_BIN checkout $SVN_CHECKOUT_OPTIONS $TARGET_COMMIT_REPS $SRV_TEMP_PATH
	sleep 1
	
	$SVN_BIN export $SVN_EXPORT_OPTIONS $EXPORT_XML_REPS $SRV_TEMP_PATH/scene.xml
	sleep 1
	
	$SVN_BIN commit $SVN_COMMIT_OPTIONS $SRV_TEMP_PATH
	rm -rf $SRV_TEMP_PATH
}

cmd_restart() {
	/etc/init.d/mz_socket restart
}

__check_modified() {
		DIFF_OUTPUT=`$SVN_BIN diff $SVN_DIFF_OPTIONS -r $PREV:$REV $SVN_RESP`

		OLDIFS=$IFS
		export IFS=$'\n'
		
		SUCCESS=0
		for line in $DIFF_OUTPUT
		do
			RET=$(echo $line |grep "$SVN_MATCH_PATTERN")
			if [ "$RET" != "" ]
			then
				SUCCESS=1
				break
			fi
		done

		export IFS=$OLDIFS
		if [ $SUCCESS == 1 ]
		then
			return 1
		fi
		return 0
}

__check_xml_modified() {
		DIFF_OUTPUT=`$SVN_BIN diff $SVN_DIFF_OPTIONS -r $PREV:$REV $SVN_RESP`

		OLDIFS=$IFS
		export IFS=$'\n'
		
		SUCCESS=0
		for line in $DIFF_OUTPUT
		do
			RET=$(echo $line |grep "$XML_MATCH_PATTERN")
			if [ "$RET" != "" ]
			then
				SUCCESS=1
				break
			fi
		done

		export IFS=$OLDIFS
		if [ $SUCCESS == 1 ]
		then
			return 1
		fi
		return 0
}

__check_restart_command() {
		LOG_OUTPUT=`$SVN_BIN log $SVN_PWD_OPTIONS -r $REV $SVN_RESP `
		
		OLDIFS=$IFS
		export IFS=$'\n'
		
		SUCCESS=0
		for line in $LOG_OUTPUT
		do
			RET=$(echo $line |grep "@restart")
			if [ "$RET" != "" ]
			then
				SUCCESS=1
				break
			fi
		done
		
		export IFS=$OLDIFS
		if [ $SUCCESS == 1 ]
		then
			return 1
		fi
		return 0
}

__check_modified
NEED_EXPORT=$?

__check_xml_modified
NEED_XML_EXPORT=$?

__check_restart_command
NEED_RESTART=$?

log "process hook event [start]"

if [ $NEED_EXPORT == 1 ]
then
		svn_data_export
		log "svn export .data has successfully."
fi

if [ $NEED_XML_EXPORT == 1 ]
then
		svn_xml_export
		log "svn export scene.xml has successfully."
fi

if [ $NEED_RESTART == 1 ]
then
		cmd_restart
		log "execute restart command has successfully."
fi
log "process hook event [completed]"

